//获取所有的城市
$.get("/home/allCity",function (data) {
    for(var i=0;i<data.length;i++){
        appendTag($(".city").find(".bootstrap-tagsinput"),data[i].data);
    }
});

//绑定密码验证事件
$("#pwd").find("input[name='pwd']").on('input',validationPwd);
$("#pwd").find("input[name='rpwd']").on('input',validationRpwd);

//刷新出翻译员
flushTable("/manage/selectAllProfile",JSON.stringify({"page":1}),"#page","#card");

//获取所有的语言
$.get("/home/allLanguage",function (data) {
    for(var i=0;i<data.length;i++){
        appendTag($(".language").find(".bootstrap-tagsinput"),data[i].data);
    }
});

//添加一个标签
function appendTag(parent, content) {
    var html=$("<span class='tag label label-info'>"+content+"<span data-role='remove'></span></span>");

    html.click(delOption);
    parent.append(html);
}

//发送删除请求
function delOption() {
    if(!confirm("你确定要删除吗？")){
        return;
    }
    var url;
    var json;
    var node=$(this);
    if(node.parents(".city").length){
        url="/manage/deleteCity";
        json={"city":node.text()}
    }else {
        url="/manage/deleteLanguage";
        json={"language":node.text()}
    }

    $.get(url,json,function (data) {
        if(data.code==0){
            alert(data.msg);
        }else {
            node.remove();
        }
    })
}

//添加一个城市或语言
function addCity() {
    var cityName=$("input[name='city-name']").val();
    $.get("/manage/addCity",{"city":cityName},function (data) {
        if(data.code==1){
            appendTag($(".city").find(".bootstrap-tagsinput"),cityName);
            $("input[name='city-name']").val("");
        }else {
            alert(data.msg);
        }
    })
}

//添加一种语言
function addLanguage() {
    var languageName=$("input[name='language-name']").val();
    $.get("/manage/addLanguage",{"language":languageName},function (data) {
        if(data.code==1){
            appendTag($(".language").find(".bootstrap-tagsinput"),languageName);
            $("input[name='language-name']").val("");
        }else {
            alert(data.msg);
        }
    })
}

//分页系统的addOne函数
function addOne(data) {
    var html=$("<div class='col-sm-3'>\n" +
        "    <div class='thumbnail'>\n" +
        "        <p class='text-right'>\n" +
        "            <button class='btn btn-sm btn-danger'>\n" +
        "                <span class='glyphicon glyphicon-remove'></span>\n" +
        "            </button>\n" +
        "        </p>\n" +
        "        <img src="+data.image+" class='img-card'>\n" +
        "        <div class='caption text-center'>\n" +
        "            <h4><a href='/html/admin/personage.html?id="+data.id+"'>"+data.name+"</a></h4>\n" +
        "            <p>ID:<span class='profile-id'>"+data.id+"</span></p>\n" +
        "            <p class='text-center'>pwd: "+data.pwd+"</p>" +
        "            <div class='text-center switch'>\n" +
        "                <span>是否显示：<input type='checkbox' data-toggle='switch'></span>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>");

    if(data.visit==1){
        html.find("input").attr("checked","checked");
    }

    var input=html.find("input[type='checkbox']");
    input.bootstrapSwitch('onSwitchChange',changeVisit);
    input.on('switch-change', changeVisit);

    html.find(".btn-danger").click(deleteProfile);
    html.find("input[type='checkbox']").change(changeVisit);

    $("#card").append(html);
}

//删除一个翻译员
function deleteProfile() {
    if(confirm("你确定要删除这个翻译员吗？删除之后所有数据都将不可恢复！")){
        var parent=$(this).parents('.col-sm-3');
        var id=parent.find(".profile-id").text();

        $.get("/manage/DeleteProfile",{id:id},function (data) {
            if(data.code==0){
                alert(data.msg);
            }else {
                parent.remove();
            }
        })
    }
}

//更改是否显示
function changeVisit() {
    var parent=$(this).parents('.col-sm-3');
    var id=parent.find(".profile-id").text();
    var status;
    if(parent.find("input[type='checkbox']").attr("checked")){
        parent.find("input[type='checkbox']").attr("checked",false);
        status=0;
    }else{
        parent.find("input[type='checkbox']").attr("checked",true);
        status=1;
    }

    $.get("/manage/changeShowProfile",{id:id,status:status},function (data) {
        if(data.code==0){
            alert(data.msg);
        }
    });
}

//新增一个翻译人
function addProfile() {
    $.get("/manage/addProfile",function (data) {
        if(data.code){
            location.reload();
        }else {
            data.msg;
        }
    });
}

//验证密码
function validationPwd() {
    var pwd=$("#pwd").find("input[name='pwd']").val();
    if(pwd.length<8){
        $(".sign-pwd").html("<span style='color: red' class='glyphicon glyphicon-remove'></span>");
        return false;
    }else {
        $(".sign-pwd").html("<span style='color: green' class='glyphicon glyphicon-ok'></span>");
        return true;
    }
}


function validationRpwd() {
    var pwd=$("#pwd").find("input[name='pwd']").val();
    var rpwd=$("#pwd").find("input[name='rpwd']").val();
    if(pwd===rpwd){
        $(".sign-rpwd").html("<span style='color: green' class='glyphicon glyphicon-ok'></span>");
        return true;
    }else {
        $(".sign-rpwd").html("<span style='color: red' class='glyphicon glyphicon-remove'></span>");
        return false;
    }
}

//修改管理员密码
function changeMistake() {
    if(!validationPwd()){
        alert("密码要大于8位");
        return;
    }else if(!validationRpwd()){
        alert("两次密码输入不一致");
        return;
    }

    var json={oldPwd:$("#pwd").find("input[name='oldPwd']").val(),pwd:$("#pwd").find("input[name='pwd']").val()};

    $.get("/manage/changeRootPwd",json,function (data) {
        if(data.code==0){
            alert(data.msg);
        }else {
            $("#pwd").modal("hide");
        }
    });
}